package com.glorysoft.hlgdpda.ui.login.view

import android.os.Bundle
import android.view.View
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseActivity
import com.glorysoft.hlgdpda.databinding.ActivityLoginBinding
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.ui.login.viewmodel.LoginViewModel
import com.glorysoft.hlgdpda.ui.main.view.MainActivity
import com.glorysoft.hlgdpda.ui.setting.view.SettingActivity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_login.*
import pub.devrel.easypermissions.AppSettingsDialog
import pub.devrel.easypermissions.EasyPermissions

/**
 * FileName: LoginActivity
 * Author: XJH
 * Date: 2021/5/12 18:37
 * Description:
 * History:
 */
class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() , EasyPermissions.PermissionCallbacks, View.OnClickListener {
    override fun layoutId(): Int {
        return R.layout.activity_login
    }

    override fun initVariableId(): Int {
        return BR.loginViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        viewModel.initUser(this)
        viewModel.defUI.startActivity.observe(this){
            startActivity(MainActivity::class.java)
            finish()
        }
    }

    override fun initData() {
        btnLogin.setOnClickListener(this)
        ivSetting.setOnClickListener(this)
    }

    override fun onPermissionsGranted(requestCode: Int, perms: MutableList<String>?) {

    }

    override fun onPermissionsDenied(requestCode: Int, perms: MutableList<String>?) {
        viewModel.defUI.toastEvent.value = ToastMessageBean("用户授权失败", false)
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms!!)) {
            AppSettingsDialog.Builder(this).build().show()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.btnLogin ->viewModel.login()
            R.id.ivSetting ->startActivity(SettingActivity::class.java)
        }
    }
}